package HttpServlet;

import Entity.userinformation;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@WebServlet(urlPatterns = {"/upload1"})
public class loginservlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*
        Jsonb jsonb = JsonbBuilder.create();
        String postJsonString = new BufferedReader(new InputStreamReader(request.getInputStream()))
                .lines().collect(Collectors.joining(System.lineSeparator()));
        userinformation dataDTO = jsonb.fromJson(postJsonString, userinformation.class);
        */

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        int flag = 1;
        FileInputStream fis = null;
        BufferedReader br = null;
        //Construct BufferedReader from InputStreamReader
        try {
            fis = new FileInputStream("src/main/LOGIN.txt");
            br = new BufferedReader(new InputStreamReader(fis));
            String line = null;
            Jsonb jsonb2 = JsonbBuilder.create();
            while ((line = br.readLine()) != null) {
                userinformation personDTO = jsonb2.fromJson(line, userinformation.class);
                if(personDTO.getUsername().equals(username) && personDTO.getPassword().equals(password))
                {
                    flag = 0;
                    HttpSession session = request.getSession(true);
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("用户名",personDTO.getUsername());
                    map.put("密码",personDTO.getPassword());
                    map.put("学号",personDTO.getStu_number());
                    map.put("手机号码",personDTO.getPhonenumber());
                    map.put("电子邮箱",personDTO.getEmail());
                    //HttpSession session = request.getSession(true);
                    session.setAttribute("userName", map);
                    session.setAttribute("token", "not null");
                    break;
                }
            }
        } catch (IOException e) {
            response.sendRedirect("/error.html");
        }
        finally {
            try {
                fis.close();
                br.close();
            } catch (IOException e) {
                response.sendRedirect("/error.html");
            }
        }

        if(flag == 0) {
            response.sendRedirect("/show.jsp");
        }
        else {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("账号或密码不正确！！请退回上一步继续操作");
        }

    }
}
